﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Timers;
using DOAN.DO;
using DOAN.BL;

namespace DOAN
{
    public partial class Baithi : System.Web.UI.Page
    {
        string dad = "";
        string datl = "";
        public double seconds;

        protected void Page_Load(object sender, EventArgs e)
        {
            seconds = 1 * 60;
            if (!IsPostBack)
            {
                lblLoi.Text = "";
                loadMon();
            }
        }

        public void loadMon()
        {
            drpMon.DataSource = new MonhocBL().SelectAll();
            drpMon.DataTextField = "Tenmon";
            drpMon.DataValueField = "Mamonhoc";
            drpMon.DataBind();
        }

        private string getcauhoi(int id)
        {
            return new DethiBL().SelectDeThi(new DethiDO { Madethi = id }, Session["Tendangnhap"].ToString()).Macauhoi;
        }

        public BailamDO init()
        {
            BailamDO obj = new BailamDO();
            obj.Madethi = int.Parse(txtma.Text);
            obj.Ngaythi = DateTime.Now.ToShortDateString();
            obj.Tentaikhoan = Session["Tendangnhap"].ToString();
            return obj;
        }

        protected void btoke_Click(object sender, EventArgs e)
        {
            BailamDO objBailam = init();
            btXemdapan.Visible = true;
            for (int i = 0; i < dtCauhoi.Items.Count; i++)
            {
                string tem = "";
                RadioButton rdA = (RadioButton)dtCauhoi.Items[i].FindControl("Rd1");
                RadioButton rdB = (RadioButton)dtCauhoi.Items[i].FindControl("Rd2");
                RadioButton rdC = (RadioButton)dtCauhoi.Items[i].FindControl("Rd3");
                RadioButton rdD = (RadioButton)dtCauhoi.Items[i].FindControl("Rd4");
                if (rdA.Checked == true)
                    tem = "A-";
                else if (rdB.Checked == true)
                    tem = "B-";
                else if (rdC.Checked == true)
                    tem = "C-";
                else
                    tem = "D-";
                datl += tem;

            }
            objBailam.Traloi = datl;
            
            string[] separator = new string[] { "-" };

            string[] tl = datl.Split(separator, StringSplitOptions.RemoveEmptyEntries);
            string[] da = Session["DAD"].ToString().Split(separator, StringSplitOptions.RemoveEmptyEntries);
            int socaudung = 0;
            for (int j = 0; j < da.Length; j++)
            {
                if (tl[j] == da[j])
                {
                    socaudung++;
                }
            }

            lbda.Text = "Dap an dung:" + Session["DAD"].ToString() + "&&& Dap an tra loi: " + datl;
            lblTT_Socaudung.Text = "<span style='color:Red;' ><b><i>" + socaudung.ToString() + "</i></b></span>";
            lblTT_Sodiem.Text = "<span style='color:Red;' ><b><i>" + (float)(socaudung* (100) / da.Length) + "</i></b></span>";
            
            objBailam.Tongdiem = (double)(socaudung * (100) / da.Length);
            
            new BailamBL().Insert(objBailam);
            btNopbai.Visible = false;
            Timer1.Enabled = false;
            btbegin.Visible = true;
        }

        protected void btbegin_Click(object sender, EventArgs e)
        {
            pnTT.Visible = true;
            pnthi.Visible = true;

            if (Session["Tendangnhap"] != null)
            {
                ThanhvienDO obj = new ThanhvienBL().Select(new ThanhvienDO { Tentaikhoan = Session["Tendangnhap"].ToString() });
                lblTT_hoten.Text = "<span style='color:Red;' ><b><i>" + obj.Hovaten+"</i></b></span>";
            }
            else
            {
                Response.Redirect("Dangnhap.aspx");
                //lblTT_hoten.Text = "<span style='color:Red;' >Khach</span>";
            }
            lblTT_monthi.Text = drpMon.SelectedItem.Text;
            try
            {
                string macauhoi = getcauhoi(int.Parse(txtma.Text));
                DethiDO objDeThi = new DethiBL().SelectDeThi(new DethiDO { Madethi = int.Parse(txtma.Text) }, Session["Tendangnhap"].ToString());
                lbTG.Text = "<b>Tuhoi gian: </b>" + objDeThi.Thoigian.ToString();
                seconds = Convert.ToDouble(int.Parse(objDeThi.Thoigian.ToString())*60);
                Timer1.Enabled = true;
                Timer1.Interval = Convert.ToInt32(int.Parse(objDeThi.Thoigian.ToString()) * 60)*1000;
                //Timer1.s
                DataTable dt = new CauhoiBL().GetCH(macauhoi);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dad += dt.Rows[i].ItemArray[11].ToString() + "-";
                }
                if (dt.Rows.Count > 0)
                {
                    dtCauhoi.DataSource = dt;
                    dtCauhoi.DataBind();
                }
                Session["DAD"] = dad;
                btNopbai.Visible = true;
                btbegin.Visible = false;
            }
            catch (Exception eee)
            {
                lblLoi.Text = "Ma de thi ko chinh xac hoặc bạn đã làm đề thi này!";
            }
            
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            btoke_Click(sender, e); 
        }

        //void timer1_elapse(whatever obj, whatever args)
        //{
        //    response.write("It has been 10 seconds, firing now");
        //    Timer1.stop(); // will stop the timer, so it will not execute every 10 seconds.
        //}
    }
}
